package com.qingcheng.controller;

import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.TbTemplate;
import com.qingcheng.pojo.goods.Template;
import com.qingcheng.service.TbTemplateService;
import com.qingcheng.service.TemplateService;
import com.qingcheng.util.PageUtil;
import com.qingcheng.util.YouthR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * @author Simon
 * @date 2020/07/27
 */
@Slf4j
@RestController
@RequestMapping("/template")
@Api(tags = "TbTemplateController")
public class TemplateController {

    @Autowired
    private TemplateService templateService;


    @Resource
    private TbTemplateService tbTemplateService;

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public YouthR detail(@PathVariable Integer id) {
        TbTemplate tbTemplate = tbTemplateService.findById(id);
        return YouthR.ok().data("tbTemplate",tbTemplate);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public YouthR list() {
        List<TbTemplate>  list = tbTemplateService.queryAll();
        return YouthR.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages")
    public YouthR listPages(Integer pageNum,Integer pageSize) {
        PageUtil<TbTemplate> pageUtils = tbTemplateService.findListAll(pageNum, pageSize);

        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public YouthR pageQueryByAll(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize,
                                 @RequestBody TbTemplate tbTemplate){
        PageUtil<TbTemplate> pageUtils = tbTemplateService.pageQueryByAll(pageNum, pageSize, tbTemplate);
        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/addTemplate")
    public YouthR addTemplate(@RequestBody TbTemplate tbTemplate) {

        boolean b = tbTemplateService.saveTbTemplate(tbTemplate);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public YouthR update(@RequestBody TbTemplate tbTemplate) {
        boolean b = tbTemplateService.updateTbTemplate(tbTemplate);

        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete")
    public YouthR delete(@RequestParam("id") Integer id) {
        boolean b = tbTemplateService.deleteById(id);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @GetMapping("/findAll")
    public List<Template> findAll(){
        return templateService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Template> findPage(int page, int size){
        return templateService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Template> findList(@RequestBody Map<String,Object> searchMap){
        return templateService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Template> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  templateService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public Template findById(Integer id){
        return templateService.findById(id);
    }



}
